package com.zh.third.service.impl;

import com.zh.common.domain.UserDO;
import com.zh.common.utils.ShiroUtils;
import com.zh.third.dao.SiteDao;
import com.zh.third.domain.SiteDO;
import com.zh.third.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SiteServiceImpl implements SiteService {
	@Autowired
	private SiteDao siteDao;
	
	@Override
	public SiteDO get(String id){
		return siteDao.get(id);
	}

	@Override
	public SiteDO getDefault(String userId) {
		return siteDao.getDefault(userId);
	}

	@Override
	public List<SiteDO> list(Map<String, Object> map){
		return siteDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return siteDao.count(map);
	}
	
	@Override
	public int save(SiteDO site){
		return siteDao.save(site);
	}
	
	@Override
	public int update(SiteDO siteDO){
		UserDO user = ShiroUtils.getUser();

		SiteDO aDefault = siteDao.getDefault(user.getUserId());
		if (aDefault != null) {
			aDefault.setIsDefault(0);
			aDefault.setUpdateBy(user.getName());
			aDefault.setUpdateDate(new Date());
			siteDao.update(aDefault);
		}

		siteDO.setIsDefault(1);
		siteDO.setUpdateBy(user.getName());
		siteDO.setUpdateDate(new Date());

		return siteDao.update(siteDO);
	}
	
	@Override
	public int remove(String id){
		return siteDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return siteDao.batchRemove(ids);
	}
	
}
